<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\AdminModel;
use App\Model\UserModel;

class UserController extends Controller
{
    //用户列表
    public function index() {
        //获取所有用户
        $userModel = new UserModel;
        $users = $userModel->getUsers();

        return view('admin.user.index',compact('users'));
    }

    //修改密码
    public function pass(Request $request) {
        //判断是否是post请求
        if ($request->isMethod('post')) {
            $new_pass = trim($request->input('newpass'));
            $username = session('username');

            //模型修改密码
            $admin_model = new AdminModel;

            $res = $admin_model->updatePass($username , $new_pass);

            if ($res) {
                return ['msg' => '修改密码成功','status' => 'success'];
            } else {
                return ['msg' => '修改密码失败','status' => 'fail'];
            }
        }
        return view('admin.user.pass');
    }

    //用户冻结
    public function freeze($id) {
        $userModel = new UserModel;
        $res = $userModel->freezeUser($id);

        if ($res) {
            return ['msg' => '冻结成功','status' => 'success'];
        } else {
            return ['msg' => '解冻成功','status' => 'fail'];
        }
    }
}
